package ca.jerg.data;

import java.util.*;

public class ErgDataStep implements IErgDataShape
{
	public static void main( String[] args )
	{
		ErgDataStep eds = new ErgDataStep(10.0,100,500);
		eds.write();
		System.out.println("data points = " + eds.getNumberOfPoints());
		
		eds.setData(FIRST,400.0,600);
		eds.write();
		System.out.println(eds.getData() == null);
	}

	public ErgDataStep( double time, int startWatts, int endWatts )
	{
		data = new ArrayList<ErgPoint>();
		
		ErgPoint ep = new ErgPoint(time,startWatts);
		data.add(ep);
		
		ep = new ErgPoint(time,endWatts);
		data.add(ep);
	}
	
	public int getNumberOfPoints()
	{
		return data.size();
	}

	public ErgPoint getPoint( int index )
	{	
		if ( index > data.size() - 1 )
			return null;
		
		if ( index == LAST )
			index = data.size() - 1;
		
		return data.get(index);
	}

	public ArrayList<ErgPoint> getData()
	{
		ErgPoint start = data.get(0);
		ErgPoint stop = data.get(1);
		
		// a STEP function has 2 points with the same time but different watts
		// if the function has been modified such that this isn't the case any
		// more then return null
		if ( start.getTime() != stop.getTime() )
			return null;
		
		return data;
	}
	
	//@Override
	public boolean setData( int index, double time, int watts )
	{		
		if ( index > 1 )
			return false;
						
		ErgPoint ep = data.get(index);
		ep.setTime(time);
		ep.setWatts(watts);
		
		return true;
	}

	//@Override
	public boolean setData( int index, ErgPoint point )
	{	
		if ( index > (data.size() - 1) )
			return false;
		
		ErgPoint ep = data.get(index);
		ep.setTime(point.getTime());
		ep.setWatts(point.getWatts());
		
		return true;
	}

	//@Override
	public void write()
	{		
		Iterator i = data.iterator();
		
		while ( i.hasNext() )
		{
			ErgPoint ep = (ErgPoint)i.next();
			ep.write();
		}
	}

	private ArrayList<ErgPoint> data;
}
